package com.jxx.controller;

import com.jxx.pojo.Item;
import com.jxx.pojo.Shopcart;
import com.jxx.pojo.User;
import com.jxx.service.ShopcartService;
import com.jxx.vo.SysResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
public class ShopcartController {
    @Autowired
    private ShopcartService shopcartService;


    /**
     * 显示页面
     * @param model
     * @return
     */
    @RequestMapping("shopcart")
    public String shopcart(Model model){
        //根据用户名查询有几购物车数据
        Long userId=7L;
        //List<> shopcartService.findshopCart();
        //将查询数据返回到页面
        return "/WebStage/shopcart/cart";
    }

    /**
     * 根据用户查询商品数据
     * @param
     * @return
     */
    @RequestMapping("findCartListUserById")
    @ResponseBody
    public List<Item> findCart(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<Item> list=shopcartService.findShoppingItem(user.getUserId());
    return list;
    }

    /**
     * 添加商品
     * @param item
     */
    @RequestMapping("addshopcart")
    @ResponseBody
    public void addshopcart(Item item){
        //shopcartService.insertcart(item);
        //

        System.out.println("添加成功");
    }

    /**
     * 商品购物车添加,更新
     * @param shopcart
     */
    @PostMapping("addShopCart")
    @ResponseBody
    public void addShopCart(@RequestBody Shopcart shopcart){
       shopcartService.addShopCart(shopcart);
    }

    /**
     * 多选删除
     * @param item
     * @return
     */
    @RequestMapping("deleteItemIds")
    @ResponseBody
    public  String deleteItem(Long [] item){
        for(int i=0;i<item.length;i++){
            System.out.println(item[i]);
        }
        shopcartService.deleteItemId(item);
        return "数组删除成功";
    }

    /**
     * 单独删除
     * @param Itemid
     * @return
             */
    @RequestMapping("deleteItemId/{Itemid}")
    public String deleteItem(@PathVariable  Integer Itemid){
        System.out.println("进来了");
        shopcartService.deleteItem(Itemid);
        return "redirect:/shopcart";
    }
    /**
     * 业务描述:
     *  完成购物车数量的修改操作
     *  url地址:  http://www.jt.com/cart/update/num/1474392004/4
     *  参数:     restFul风格
     *  返回值:   void
     */
//    @RequestMapping("/cart/update/num/{itemId}/{num}")
//    @ResponseBody  //让ajax程序结束
//    public void updateNum(Shopcart shopcart){//springmvc 针对restFul提供的功能 名称和属性一致
//
//        Integer userId = UserThreadLocal.get().getUserId();
//        shopcart.setUserId(userId);
//        shopcartService.updateCartNum(shopcart);
//    }

}
